﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MusicLibrary.DataModels;
using MusicLibrary.Web.ViewModels;
using MusicLibrary.Data;

namespace MusicLibrary.Web.DbAdapters
{
    public class MusicLibraryAdapter : IMusicLibraryAdapter
    {

        public List<AlbumListVm> GetAlbumList()
        {
            List<AlbumListVm> albums = new List<AlbumListVm>();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                albums = db.Albums.Select(x => new AlbumListVm()
                {
                    Id = x.Id,
                    Title = x.Title,
                    Artist = x.Artist,
                    ImageUrl = x.ImageUrl
                }).ToList();
            }
            return albums;
        }


        public AlbumDetailVm GetAlbumDetail(int id)
        {
            AlbumDetailVm album = new AlbumDetailVm();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                // LINQ statement gets the album with a matching Id
                // Transforms that to album detail view model
                // Fills list of tags by going through TagAlbums intermediate table
                // Fills in list of Songs
                // Fills in list of tags for each song

                album = db.Albums
                    .Where(dbAlbum => dbAlbum.Id == id)
                    .Select(dbAlbum => new AlbumDetailVm()
                {
                    Id = dbAlbum.Id,
                    Title = dbAlbum.Title,
                    Artist = dbAlbum.Artist,
                    ImageUrl = dbAlbum.ImageUrl,
                    Tags = db.TagAlbums
                    .Where(dbTagAlbum => dbTagAlbum.AlbumId == dbAlbum.Id).ToList()
                    .Select(dbTagAlbum => new TagVm()
                    {
                        Id = dbTagAlbum.TagId,
                        Name = db.Tags
                        .Where(dbTag => dbTag.Id == dbTagAlbum.TagId).FirstOrDefault()
                        .Name
                    }).ToList(),
                    Songs = db.Songs
                    .Where(dbSong => dbSong.AlbumId == dbAlbum.Id)
                    .Select(dbSong => new SongListVm()
                    {
                        Id = dbSong.Id,
                        Title = dbSong.Title,
                        Tags = db.TagSongs
                        .Where(dbTagSong => dbTagSong.SongId == dbSong.Id)
                        .Select(dbTagSong => new TagVm()
                        {
                            Id = dbTagSong.TagId,
                            Name = db.Tags
                            .Where(dbTag => dbTag.Id == dbTagSong.TagId).FirstOrDefault().Name
                        }).ToList()
                    }).ToList()
                }).FirstOrDefault();
            }

            return album;
        }


        public AlbumListVm SaveNewAlbum(AlbumListVm albumVm)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Album album = new Album(albumVm.Title, albumVm.Artist, albumVm.ImageUrl);
                album = db.Albums.Add(album);
                db.SaveChanges();

                albumVm.Id = album.Id;
            }
            return albumVm;
        }


        public AlbumListVm UpdateAlbum(AlbumListVm albumVm)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                Album album = db.Albums.Find(albumVm.Id);
                album.Title = albumVm.Title;
                album.Artist = albumVm.Artist;
                album.ImageUrl = albumVm.ImageUrl;
                db.SaveChanges();

            }
            return albumVm;
        }


        public AlbumListVm AddTagToAlbum(int albumId, string tagValue)
        {
            AlbumListVm albumVm = new AlbumListVm();
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                // get album
                // check if tag exists
                // if exists, get id
                // make sure it is not already assigned
                // if doesn't exist, create it, get id
                // create new TagAlbum with album id and tag id

                Album album = db.Albums.Find(albumId);

                if (db.Tags.Where(x => x.Name.Contains(tagValue)).ToList().Count() > 0)
                {
                    Tag tag = db.Tags.Where(x => x.Name.Contains(tagValue)).FirstOrDefault();

                    if (db.TagAlbums.Where(x => x.TagId == tag.Id && x.AlbumId == album.Id).ToList().Count() < 1)
                    {
                        TagAlbum ta = new TagAlbum(tag.Id, album.Id);
                        ta = db.TagAlbums.Add(ta);
                        db.SaveChanges();

                    }
                }
                else
                {
                    Tag tag = db.Tags.Add(new Tag(tagValue));
                    db.SaveChanges();

                    TagAlbum ta = new TagAlbum(tag.Id, album.Id);
                    ta = db.TagAlbums.Add(ta);
                    db.SaveChanges();


                }

                albumVm.Id = album.Id;
                albumVm.Title = album.Title;
                albumVm.Artist = album.Artist;
                albumVm.ImageUrl = album.ImageUrl;


            }
            return albumVm;
        }
    }
}